package exam.wangyi8_21;

import java.util.Deque;
import java.util.LinkedList;

public class test5 {
    public static int maxAreaOfIsland(int[][] grid) {
        int max = 0;
        int [][] pats = {
                {1,0,-1,0},
                {0,1,0,-1}
        };
        if(grid.length==0){
            return 0;
        }
        int m = grid.length;
        int n = grid[0].length;
        int [][] used = new int[m][n];

        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if(grid[i][j]==1&&used[i][j]==0){
                    Deque<int[]> q = new LinkedList<int[]>();
                    int area = 0;
                    q.add(new int[] {i,j});
                    while(!q.isEmpty()){
                        int size = q.size();
                        while(size-->0){
                            int[]k = q.pollFirst();
                            int x = k[0];
                            int y = k[1];
                            used[x][y] = 1;
                            area++;
                            for(int l = 0;l<4;l++){
                                int x2 = x+pats[0][l];
                                int y2 = y+pats[1][l];
                                if(x2<0||x2>=m||y2<0||y2>=n){
                                    continue;
                                }
                                if(grid[x2][y2]==1&&used[x2][y2]==0){
                                    q.add(new int[] {x2,y2});
                                    used[x][y] = 1;
                                }
                            }
                        }
                    }
                    max = Math.max(max,area);
                }
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int[][] in = {{1,1,0,0,0},{1,1,0,0,0},{0,0,0,1,1},{0,0,0,1,1}};
        int i = maxAreaOfIsland(in);
        System.out.println(i);

    }
}
